import 'package:flutter/material.dart';
import './myitems.dart';
class ReportApp extends StatelessWidget {
  const ReportApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        ItemArgumentsScreen.routeName: (context) =>
            const ItemArgumentsScreen(),
      },
      // Provide a function to handle named routes.
      // Use this function to identify the named
      // route being pushed, and create the correct
      // Screen.
      onGenerateRoute: (settings) {
        // If you push the PassArguments route
        if (settings.name == PassItemArgumentsScreen.routeName) {
          // Cast the arguments to the correct
          // type: ItemArguments.
          final args = settings.arguments as ItemArguments;

          // Then, extract the required data from
          // the arguments and pass the data to the
          // correct screen.
          return MaterialPageRoute(
            builder: (context) {
              return PassItemArgumentsScreen(
                id: args.id,
                name: args.name,
              );
            },
          );
        }
        // The code only supports
        // PassItemArgumentsScreen.routeName right now.
        // Other values need to be implemented if we
        // add them. The assertion here will help remind
        // us of that higher up in the call stack, since
        // this assertion would otherwise fire somewhere
        // in the framework.
        assert(false, 'Need to implement ${settings.name}');
        return null;
      },
      title: 'Navigation with Arguments',
      home: ItemsHome(),
    );
  }
}

class ItemHomeScreen extends StatelessWidget {
  const ItemHomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home Screen'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // A button that navigates to a named route.
            // The named route extracts the arguments
            // by itself.
            ElevatedButton(
              onPressed: () {
                // When the user taps the button,
                // navigate to a named route and
                // provide the arguments as an optional
                // parameter.
                Navigator.pushNamed(
                  context,
                  ItemArgumentsScreen.routeName,
                  arguments: ItemArguments(
                    1,
                    'another from parent',
                  ),
                );
              },
              child: const Text('Navigate to screen that extracts arguments'),
            ),
            // A button that navigates to a named route.
            // For this route, extract the arguments in
            // the onGenerateRoute function and pass them
            // to the screen.
            ElevatedButton(
              onPressed: () {
                // When the user taps the button, navigate
                // to a named route and provide the arguments
                // as an optional parameter.
                Navigator.pushNamed(
                  context,
                  PassItemArgumentsScreen.routeName,
                  arguments: ItemArguments(
                    2,
                    'This message is extracted in the onGenerateRoute '
                        'function.',
                  ),
                );
              },
              child: const Text('Navigate to a named that accepts arguments'),
            ),
          ],
        ),
      ),
    );
  }
}

// A Widget that extracts the necessary arguments from
// the ModalRoute.
class ItemArgumentsScreen extends StatelessWidget {
  const ItemArgumentsScreen({super.key});

  static const routeName = '/extractItemArguments';

  @override
  Widget build(BuildContext context) {
    // Extract the arguments from the current ModalRoute
    // settings and cast them as ItemArguments.
    final args = ModalRoute.of(context)!.settings.arguments as ItemArguments;
    final TextEditingController name_Controller = new TextEditingController(text: args.name);
    return Scaffold(
      appBar: AppBar(
        title: Text("edit item"),
      ),
      body: Center(
        child: 
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            //第一行Icon使用主题中的iconTheme
            //Row(
            //    mainAxisAlignment: MainAxisAlignment.center,
            //    children: <Widget>[
            //      Icon(Icons.favorite),
            //      Icon(Icons.airport_shuttle),
            //      Text("  颜色跟随主题")
            //    ]
            //),
            Text(args.id.toString()),
            TextField(controller:  name_Controller,
              decoration: InputDecoration(labelText: "name"),
              ),
            ElevatedButton(
            onPressed: (){
              //puti(res["data"][i]["id"],res["data"][i]["name"]);
              // put("/rest/Item/", {"id":args.id,"name":name_Controller.text},(r){
              //   print(r);
              // }, (e){
              //   print(e);
              // });
              // Navigator.of(context).pop();
              //delete 
            }, child: Text("Ok")),
            ElevatedButton(
            onPressed: (){
              Navigator.of(context).pop();

            }, child: Text("Cancel"))
            ]
      )),);
  }
}

// A Widget that accepts the necessary arguments via the
// constructor.
class PassItemArgumentsScreen extends StatelessWidget {
  static const routeName = '/passItemArguments';

  final int id;
  final String name;

  // This Widget accepts the arguments as constructor
  // parameters. It does not extract the arguments from
  // the ModalRoute.
  //
  // The arguments are extracted by the onGenerateRoute
  // function provided to the MaterialApp widget.
  const PassItemArgumentsScreen({
    super.key,
    required this.id,
    required this.name,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(id.toString()),
      ),
      body: Center(
        child: Text(name),
      ),
    );
  }
}

// You can pass any object to the arguments parameter.
// In this example, create a class that contains both
// a customizable title and message.
class ItemArguments {
  final int id;
  final String name;

  ItemArguments(this.id, this.name);
}